<?php
$this->template->add_admin_js_lib("SimpleAjaxUploader.min.js");
$this->template->add_admin_js_lib("jquery.datetimepicker.js");
$this->template->add_admin_css_lib("jquery.datetimepicker.css");
$this->template->add_admin_js_lib("tinymce/tinymce.min.js");
$game_id = (isset($edit->{$this->{$this->model}->primaryKey})?$edit->{$this->{$this->model}->primaryKey}:0);
?>
<form method="post" action="<?php echo $this->url_edit . $game_id; ?>"  enctype="multipart/form-data" class="form-horizontal">
<div class="widget-box">
	<div class="widget-title">
		<ul class="nav nav-tabs">
			<li class="active"><a data-toggle="tab" href="#tab1">Thông tin cơ bản</a></li>
			<li><a data-toggle="tab" href="#tab2">Game file</a></li>
			<li><a data-toggle="tab" href="#tab3">Hướng dẫn</a></li>
			<li><a data-toggle="tab" href="#arena">đấu trường</a></li>
			<li><a data-toggle="tab" href="#tabseo">SEO</a></li>
		</ul>
	</div>
	<div class="widget-content tab-content">
		<div id="tab1" class="tab-pane active"><div class="widget-content nopadding">
				<?php echo $this->atheme->group_input2('edit[game_title]', 'Tên game', '', 'span6 ', @$edit->game_title, 'text', ''); ?>
				<?php $link = 'Nếu để trống, hệ thống sẽ tự động phát sinh theo tên game';
					if($game_id >0){
						$game_link = game_url($edit->game_id, $edit->game_code);
						if($edit->game_type =='arena')
							$game_link = arena_url($edit->game_id, $edit->game_code);
						$link.= '<br><a href="'.$game_link.'" target="_blank" >'.$game_link.'</a>';
					}
				?>
				<?php echo $this->atheme->group_input2('edit[game_code]', 'Mã code seo', '', 'span6 ', @$edit->game_code, 'text', '',$link); ?>
				
				<?php echo $this->atheme->group_select2('cat_id[]','Danh mục','','', $cate_tree,@$game_cate,' multiple'); ?>
				<?php echo $this->atheme->group_textarea2('edit[game_content]', 'Giới thiệu', '', 'span6 ', @$edit->game_content, 'text', ''); ?>
				<?php echo $this->atheme->group_input2('edit[game_key]', 'Game Key', '', 'span6 ', @$edit->game_key, 'text', ''); ?>
				<?php 
				/*
				if($game_id > 0){
				 echo $this->atheme->group_text2('Game Key',@$edit->game_key); 
					}else {
						echo $this->atheme->group_input2('edit[game_key]', 'Game Key', '', 'span6 ',md5(uniqid(rand(), true)), 'text', 'readonly="readonly"');
					}
					*/
					?>
				
				<?php echo $this->atheme->group_input2('edit[game_view]', 'Lượt chơi', '', 'span6 ', isset($edit->game_view)?$edit->game_view:0, 'text', ''); ?>
				<?php echo $this->atheme->group_input2('edit[game_min_scores]', 'Điểm game thấp nhất', '', 'span6 ', isset($edit->game_min_scores)?$edit->game_min_scores:0, 'text', '','Số điểm của game cần đạt để được cộng điểm kinh nghiệm, mặc định: 0'); ?>
				
				<?php
				$arr_status = array();
				if($this->mgame->permission('process_game'))
				{
					$arr_status['2'] = 'Duyệt game, được thách đấu';
					$arr_status['1'] = 'Duyệt game, không thách đấu';
					$arr_status['0'] = 'Ẩn';
					$arr_status['-1'] = 'Chờ duyệt game';
					$arr_status['-2'] = 'Test lại game';
					$arr_status['-3'] = 'Sửa lại game';
				}
				else if($this->mgame->permission('post_game'))
				{
					$arr_status['-3'] = 'Lưu tạm';
					$arr_status['-2'] = 'Chờ test';
				}
				else if($this->permission->check("select", 'GAME') || $this->permission->check("update", 'GAME') || $this->permission->check("insert", 'GAME')){
					$arr_status['2'] = 'Duyệt game, được thách đấu';
					$arr_status['1'] = 'Duyệt game, không thách đấu';
					$arr_status['0'] = 'Ẩn';
					$arr_status['-1'] = 'Chờ duyệt game';
					$arr_status['-2'] = 'Test lại game';
					$arr_status['-3'] = 'Sửa lại game';
					
				}
				?>
				<?php echo $this->atheme->group_select2('edit[game_status]','Trạng thái','','', $arr_status,@$edit->game_status,'style="min-width: 300px;"'); ?>
				<?php echo $this->atheme->group_select2('edit[game_type]','Loại','','', array('game' =>'Game','arena'=>'Đấu trường'),@$edit->game_type,'style="min-width: 300px;"'); ?>
				
				<?php //echo $this->atheme->group_select2('edit[game_status]','Trạng thái','','', array('1'=>'Hiện, không thách đấu','2'=>'Hiện, được thách đấu','0'=>'Ẩn'),@$edit->game_status); ?>
				
				<div class="form-actions">
					<button type="submit" class="btn btn-primary btn-small"><?php echo (($game_id > 0)?'Save':'Add');?></button> or <a class="text-danger" href="<?php echo $this->url_index;?>">Cancel</a>
				</div>
		
		</div></div>
		<div id="tab2" class="tab-pane">
		<?php if($game_id>0): ?>
			<div class="form-group">
			<label class="control-label" for="">Game File:: </label>
				<div class="controls">
					<input type="text" id="game_file" name="" class="span3 form-control input-small btn-input-upload" value="<?php echo@$edit->game_file;?>" style="width: 450px;float: left;margin-right: 5px;"> 
					
					 <input type="button" id="game-file" class="btn  btn-primary glyphicon glyphicon-upload upload-game-file" value="Choose file" name="game_file" allowfile="swf">
					 
<span style="padding-left:5px;vertical-align:middle;"><i>SWF (30MB max file size)</i></span>
				<div id="errormsg-game-file" class="clearfix redtext">
		</div>	              
		<div id="pic-progress-wrap-game-file" class="progress-wrap" style="margin-top:10px;margin-bottom:10px;">
		</div>	

		<div id="picbox-game-file" class="clear" style="padding-top:0px;padding-bottom:10px;">
		</div>

				</div>
			</div>
			<?php echo $this->atheme->group_input2('edit[game_file_rate_height_width]', 'Tỉ lệ Height/Width', 'game_rate', 'span6 ', isset($edit->game_file_rate_height_width)?$edit->game_file_rate_height_width:1, 'text', '','Tỉ lệ Height/Width sẽ tự động cập nhật nếu up file mới lên'); ?>
			
			<div class="form-group">
				<label class="control-label" for="">Game Thumb Large: </label>
				<div class="controls">
					<img src="<?php echo image_src(@$edit->game_thumb_large);?>" class="btn-image-upload" style="width: 60px;">
					<input type="button" id="game-thumb-large" class="btn  btn-primary glyphicon glyphicon-upload upload-game-file" value="Choose file" name="game_thumb_large"  allowfile="jpg,png,gif">
					<span style="padding-left:5px;vertical-align:middle;"><i>Ảnh lớn của game, chấp nhận file PNG, JPG, or GIF (1MB max file size)</i></span>
					<div id="errormsg-game-thumb-large" class="clearfix redtext">
					</div>	              
					<div id="pic-progress-wrap-game-thumb-large" class="progress-wrap" style="margin-top:10px;margin-bottom:10px;">
					</div>	
					
					<div id="picbox-game-thumb-large" class="clear" style="padding-top:0px;padding-bottom:10px;">
					</div>
					
				</div>
			</div>
			
			<div class="form-group">
			<label class="control-label" for="">Game Thumb:: </label>
				<div class="controls">
					<img src="<?php echo image_src(@$edit->game_thumb);?>" class="btn-image-upload" style="width: 60px;">
					 <input type="button" id="game-thumb" class="btn  btn-primary glyphicon glyphicon-upload upload-game-file" value="Choose file" name="game_thumb"  allowfile="jpg,png">
		<span style="padding-left:5px;vertical-align:middle;"><i>PNG, JPG, or GIF (1MB max file size)</i></span>
		<div id="errormsg-game-thumb" class="clearfix redtext">
		</div>	              
		<div id="pic-progress-wrap-game-thumb" class="progress-wrap" style="margin-top:10px;margin-bottom:10px;">
		</div>	

		<div id="picbox-game-thumb" class="clear" style="padding-top:0px;padding-bottom:10px;">
		</div>

				</div>
			</div>
			
			
			
			<div class="form-group">
			<label class="control-label" for="">Thumb facebook: </label>
				<div class="controls">
					<img src="<?php echo image_src(@$edit->game_thumb_social);?>" class="btn-image-upload" style="width: 60px;">
					 <input type="button" id="thumb-fb" class="btn  btn-primary glyphicon glyphicon-upload upload-game-file" value="Choose file" name="game_thumb_social"  allowfile="jpg,png">
		<span style="padding-left:5px;vertical-align:middle;"><i>PNG, JPG, or GIF </i></span>
		<div id="errormsg-thumb-fb" class="clearfix redtext">
		</div>	              
		<div id="pic-progress-wrap-thumb-fb" class="progress-wrap" style="margin-top:10px;margin-bottom:10px;">
		</div>	

		<div id="picbox-thumb-fb" class="clear" style="padding-top:0px;padding-bottom:10px;">
		</div>

				</div>
			</div>
			
			
			<?php 
			 else:
			echo "Bạn phải save game rồi mới được thêm thông tin.";
			endif; ?> 
			
		<div class="form-actions">
					<button type="submit" class="btn btn-primary btn-small"><?php echo (($game_id > 0)?'Save':'Add');?></button> or <a class="text-danger" href="<?php echo $this->url_index;?>">Cancel</a>
				</div>
		</div>
		
		
		<style>.select2-container {
float: left;
margin-right: 5px;
}
.select2-container .select2-choice {
min-width: 100px;
height: 31px;
}
.controlsx {
padding: 10px 10px 10px 0;
width: 100%;
clear: both;
margin-bottom: 10px;
}
</style>
		<div id="tab3" class="tab-pane">
 <?php if($game_id>0): ?>
			 
		<div class="widget-content nopadding iframe-key-content">
		<?php 
		 
		for($i=0; $i<count($game_keys); $i++){?>
		<div class="controlsx" id="key-<?php echo $game_keys[$i]->gins_id;?>">
	<input type="text" name="keypreval[]" class="form-control input-small input-key-pre-value" value="<?php echo $game_keys[$i]->gins_pre_description;?>" style="width: 200px;float: left;">
		<select name="keykey[]" class="select2 input-key-key" id="select2" style="  ">
			<?php foreach($this->mgameinstruction->key_define as $k=>$v): ?>
				<option value="<?php echo $k;?>" <?php echo ($k == $game_keys[$i]->gins_key)?' selected="selected"':'' ?>><?php echo $v;?></option>
			<?php endforeach;?>
		
		</select> 
			<input type="text" name="keyval[]" class="form-control input-small input-key-value" value="<?php echo $game_keys[$i]->gins_description;?>" style="width: 200px;float: left;">
			 &nbsp;
			 <button type="button" class="btn btn-primary btn-small btn-key-update" data-keyid="<?php echo $game_keys[$i]->gins_id;?>">Cập nhật</button>&nbsp;
			 <button type="button" class="btn btn-danger btn-small btn-delete-key" data-keyid="<?php echo $game_keys[$i]->gins_id;?>">Xóa</button>
			</div>
		<?php } ?>
		</div>

		
		<div class="controlsx">
		<input type="text" name="" class=" form-control input-small input-key-pre-value-add" value="" style="width: 200px;float: left;">
		<select name="keykey[]" class="select2 input-key-key" id="input-add-key" style="  ">
			<?php 
			$this->mgameinstruction->key_define[0] = '--------------------------------------------';
			foreach($this->mgameinstruction->key_define as $k=>$v): ?>
				<option value="<?php echo $k;?>"><?php echo $v;?></option>
			<?php endforeach;?>
		
		</select> 
			<input type="text" name="" class=" form-control input-small input-key-value-add" value="" style="width: 200px;float: left;">
			 &nbsp;
			 <button type="button" class="btn btn-primary btn-small btn-key-add">Add Key</button>
			</div>
			
				<?php 
			 else:
			echo "Bạn phải save game rồi mới được thêm thông tin.";
			endif; ?>  
		
		</div> 
		
		
		
		<div id="arena" class="tab-pane">
		
<div class="row">
<div class="col-12">
		<div class="widget-content nopadding">
			<?php echo $this->atheme->group_input2('edit[game_time_start]', 'Ngày bắt đầu', 'time_start', 'span6 ', date("Y/m/d H:i",(isset($edit->game_time_start)?$edit->game_time_start:time())), 'text', ''); ?>
			<?php echo $this->atheme->group_input2('edit[game_time_end]', 'Ngày kết thúc', 'time_end', 'span6 ', date("Y/m/d H:i",(isset($edit->game_time_end)?$edit->game_time_end:time())), 'text', ''); ?>
			<?php echo $this->atheme->group_textarea2('edit[game_info]', 'Thể lệ', '', 'span6 textarea', @$edit->game_info, 'text', ''); ?>
			<?php /*
			<div class="form-group">
				<label class="control-label" for="">Css File: </label>
				<div class="controls">
					<input type="text" id="css_file" name="" class="span3 form-control input-small btn-input-upload" value="<?php echo@$edit->game_file;?>" style="width: 450px;float: left;margin-right: 5px;"> 
					
					<input type="button" id="css-file" class="btn  btn-primary glyphicon glyphicon-upload upload-game-file" value="Choose file" name="css_file" allowfile="css">
					<div id="errormsg-css-file" class="clearfix redtext">
					</div>	              
					<div id="pic-progress-wrap-css-file" class="progress-wrap" style="margin-top:10px;margin-bottom:10px;">
					</div>	
					
					<div id="picbox-css-file" class="clear" style="padding-top:0px;padding-bottom:10px;">
					</div>
					
				</div>
			</div>
			
			*/ ?>
				<div class="form-actions">
					<button type="submit" class="btn btn-primary btn-small"><?php echo (($game_id > 0)?'Save':'Add');?></button> or <a class="text-danger" href="<?php echo $this->url_index;?>">Cancel</a>
				</div>
			
		</div>
</div>
				
				</div>
	</div>
	<div id="tabseo" class="tab-pane">
		
<div class="row">
<div class="col-12">
		<div class="widget-content nopadding">
			
			<?php //echo $this->atheme->group_single_upload('userfile', 'Ảnh đại diện', '', ' span6', @$cate->CAT_THUMB, 'text', '80px','80px'); ?>
			
				<?php echo $this->atheme->group_input2('edit[seo_h1]', 'SEO H1', '', 'span6 ', @$edit->seo_h1, 'text', ''); ?>
				<?php echo $this->atheme->group_input2('edit[seo_description]', 'SEO DESCRIPTION', '', 'span6 ', @$edit->seo_description, 'text', ''); ?>
				<?php echo $this->atheme->group_input2('edit[seo_title]', 'SEO TITLE', '', 'span6 ', @$edit->seo_title, 'text', ''); ?>
				<?php echo $this->atheme->group_input2('edit[seo_keyword]', 'SEO KEYWORD', '', 'span6 ', @$edit->seo_keyword, 'text', ''); ?>
				
				<div class="form-actions">
				<button type="button" class="btn btn-default btn-small ping-game">Ping</button>
					<button type="submit" class="btn btn-primary btn-small"><?php echo (($game_id > 0)?'Save':'Add');?></button> or <a class="text-danger" href="<?php echo $this->url_index;?>">Cancel</a>
				</div>
			
		</div>
	 					
 
</div>
				
				</div>
	</div>                            
</div>
</div>

</form>



<script>
$(document).ready(function() {
$(document.body).on('click','.btn-delete-key', function(){
var id = $(this).attr("data-keyid");
var key = $(this).siblings(".input-key-key").find('option:selected').val();

$.ajax({
  type: "POST",
  dataType: "json",
  url: "<?php echo admin_url();?>game/instruction/delete/<?php echo @$game_id;?>",
	data: "key="+key+"&key_id="+id,
	success: function(data){
	if(data.success){
		$("#key-"+id).fadeOut().remove();
		}
		 
	}
	});
});

$(document.body).on('click',".btn-key-update",function(){
var id = $(this).attr("data-keyid");
var key = $(this).siblings(".input-key-key").find('option:selected').val();
var value = $(this).siblings(".input-key-value").val();
var pre_value = $(this).siblings(".input-key-pre-value").val();

$.ajax({
  type: "POST",
  dataType: "json",
  url: "<?php echo admin_url();?>game/instruction/update/<?php echo @$game_id;?>",
	data: "key="+key+"&key_id="+id+"&value="+value+"&prevalue="+pre_value,
	success: function(data){
	if(data.success){
		
		}
		 
	}
	});
});

	$(document.body).on('click',".ping-game",function(){
	me = $(this);
	$(this).attr("disabled","disabled");
	$(this).removeClass("ping-game");
	$(this).removeClass("btn-default");
	$(this).addClass("btn-info");
	$(this).text("Ping.................");
	$.ajax({
		type: "POST",
		dataType: "json",
		url: "<?php echo admin_url();?>game/ajax_ping/<?php echo @$game_id;?>",
		data: "",
		success: function(data){
			$(me).removeAttr("disabled");
			$(me).removeClass("btn-info");
			$(me).addClass("btn-inverse");
			$(me).text("Pinged !!");
			alert(data.msg);
		}
	});
});

$(".btn-key-add").click(function(){
var key = $("#input-add-key").val();
var value = $(".input-key-value-add").val();
var pre_value = $(".input-key-pre-value-add").val();
//var value = $(this).attr("input-key-value");
 
 $.ajax({
  type: "POST",
  dataType: "json",
  url: "<?php echo admin_url();?>game/instruction/add_key/<?php echo @$game_id;?>",
	data: "key="+key+"&value="+value+"&prevalue="+pre_value,
	success: function(data){
	if(data.success){
		mhtml = '';
		mhtml+= '<div class="controlsx" id="key-'+data.key_id+'">';
		mhtml+= '<input type="text" name="keypreval[]" class=" form-control input-small input-key-pre-value" value="'+data.key_pre_value+'" style="width: 200px;float: left;">';
		mhtml+= data.html;
		mhtml+= '<input type="text" name="keyval[]" class=" form-control input-small input-key-value" value="'+data.key_value+'" style="width: 200px;float: left;">';
		mhtml+= '<button type="button" class="btn btn-primary btn-small btn-key-update" data-keyid="'+data.key_id+'">Cập nhật</button>';
		mhtml+= '&nbsp;<button type="button" class="btn btn-danger btn-small btn-delete-key" data-keyid="'+data.key_id+'">Xóa</button>';
		mhtml+= '</div>';
		$(".iframe-key-content").append(mhtml);
		$("select.select2").select2();
		$(".input-key-value-add").val("");
		$(".input-key-pre-value-add").val("");
		
	}
		 
	}
});
});



function safe_tags( str ) {
  return String( str )
           .replace( /&/g, '&amp;' )
           .replace( /"/g, '&quot;' )
           .replace( /'/g, '&#39;' )
           .replace( /</g, '&lt;' )
           .replace( />/g, '&gt;' );
}

$('.upload-game-file').each(function( index ) {
	txt(this);
});

function txt(mthis){
	var me = $(mthis);
	var str= me.attr("allowfile");
	var btn = me.attr("id"),
	allowfile = str.split(','),
	mname = me.attr("name"),
	wrap = document.getElementById('pic-progress-wrap-'+btn),
	picBox = document.getElementById('picbox-'+btn),
	errBox = document.getElementById('errormsg-'+btn);
  var uploader = new ss.SimpleUpload({
        button: btn,
        url: '<?php echo admin_url();?>game/upload_my_file/<?php echo @$game_id;?>',
        progressUrl: '<?php echo admin_url();?>game/upload_process',
        name: mname,
        multiple: true,
        maxUploads: 1,
        maxSize: 30000,
        allowedExtensions: allowfile,
        accept: '*',
        hoverClass: 'btn-hover',
        focusClass: 'active',
        disabledClass: 'disabled',
        responseType: 'json',
        onExtError: function(filename, extension) {
          alert(filename + ' is not a permitted file type.'+"\n\n"+'Only SWF files are allowed.');
        },
        onSizeError: function(filename, fileSize) {
          alert(filename + ' is too big. (20MB max file size)');
        },        
        onSubmit: function(filename, ext) {            
           var prog = document.createElement('div'),
               outer = document.createElement('div'),
               bar = document.createElement('div'),
               size = document.createElement('div');
                       
            prog.className = 'prog';
            size.className = 'size';
            outer.className = 'progress progress-striped active';
            bar.className = 'progress-bar progress-bar-success';
            
            outer.appendChild(bar);
            prog.innerHTML = '<span style="vertical-align:middle;">'+safe_tags(filename)+' - </span>';
            prog.appendChild(size);
            prog.appendChild(outer);
            wrap.appendChild(prog); // 'wrap' is an element on the page
            
            this.setProgressBar(bar);
            this.setProgressContainer(prog);
            this.setFileSizeBox(size);      
            
            errBox.innerHTML = '';
            btn.value = 'Choose another file';
          },		
        startXHR: function() {
          // Dynamically add a "Cancel" button to be displayed when upload begins
          // By doing it here ensures that it will only be added in browses which 
          // support cancelling uploads
          var abort = document.createElement('button');
            
            wrap.appendChild(abort);
            abort.className = 'btn btn-sm btn-info';
            abort.innerHTML = 'Cancel';

            // Adds click event listener that will cancel the upload
            // The second argument is whether the button should be removed after the upload
            // true = yes, remove abort button after upload
            // false/default = do not remove
            this.setAbortBtn(abort, true);              
        },          
        onComplete: function(filename, response) {
            if (!response) {
              errBox.innerHTML = 'Unable to upload file';
              return;
            }     
            if (response.success === true) {
              picBox.innerHTML = '<div class="alert alert-success"><button class="close" data-dismiss="alert">×</button><strong>Success! </strong> The file upload success.</div>';
              $(me).siblings(".btn-image-upload").attr("src",response.file);
              $(me).siblings(".btn-input-upload").val(response.file);
			  if(response.rate)
              $("#game_rate").val(response.rate);
			  console.log(response);
            } else {
              if (response.msg)  {
                errBox.innerHTML = '<div class="alert alert-danger"><button class="close" data-dismiss="alert">×</button><strong>Error! </strong>'+response.msg+'</div>';
              } else {
                errBox.innerHTML = 'Unable to upload file';
              }
            }
          }
	});
//});
};

});
</script>

<script>
	$( "#time_start,#time_end" ).datetimepicker({
		  dateFormat: "yy-mm-dd",
    });
	tinymce.init({
		selector: ".textarea",theme: "modern",width: 680,height: 300,
		plugins: [
		"advlist autolink link image lists charmap print preview hr anchor pagebreak",
		"searchreplace wordcount visualblocks visualchars insertdatetime media nonbreaking",
		"table contextmenu directionality emoticons paste textcolor responsivefilemanager code"
		],
		toolbar1: "undo redo | bold italic underline | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | styleselect",
		toolbar2: "| responsivefilemanager | link unlink anchor | image media | forecolor backcolor  | print preview code ",
		image_advtab: true ,
		
		external_filemanager_path:"<?php echo $this->template->url_theme_admin;?>js/tinymce/plugins/filemanager/",
		filemanager_title:"Responsive Filemanager" ,
		relative_urls: true,
		document_base_url: 'http://www.tinymce.com/tryit/',
		external_plugins: { "filemanager" : "<?php echo $this->template->url_theme_admin;?>js/tinymce/plugins/filemanager/plugin.min.js"}
	});
 </script>